/**
 * @author zhaozhenjiang
 * 流数据读取
 * */

package com.auto.oracle;

import java.io.InputStream;

final class streamBuffer implements Runnable {
	private InputStream input = null;
	private Object lock_input = new Object();
	private boolean isWait = true;
	private byte[] bty = null;
	private IStreamListener listener = null;
	
	public streamBuffer(){
		bty = new byte[1024];
	}
	
	public void setStream(InputStream input){
		this.input = input;
	}
	
	public void start(){
		isWait = true;
		Thread thread = new Thread(this);
		thread.start();
	}
	
	public void relieveWait(){
		isWait = false;
		synchronized (lock_input) {
			lock_input.notify();
		}
	}
	
	public void setListener(IStreamListener listener){
		this.listener = listener;
	}

	@Override
	public void run() {
		while(true){
			try{
				if(isWait){
					synchronized (lock_input) {
						lock_input.wait();
					}
				}
				int len = 0;
				while((len = input.read(bty)) > 0){
					listener.filterStream(new String(bty,0,len));
				}
				input.close();
				isWait = true;
			}catch(Exception ex){}
		}	
	}	

	@SuppressWarnings("unused")
	private class Decompile{
		
	}

}
